package edu.cs.usask.ca.silver.businesslogic.ids;

import java.util.Date;
import java.util.UUID;

import edu.cs.usask.ca.silver.api.persistence.StorageI;
import edu.cs.usask.ca.silver.businesslogic.commands.generic.ModelVersion;
import edu.cs.usask.ca.silver.exceptions.SilverBusinessLogicErrorException;

/**
 * 
 * @author Yudi Xue
 * 
 */
public class ModelVersionID extends BaseId {

	public ModelVersionID(UUID value) throws SilverBusinessLogicErrorException {
		super(value);
	}

	// return example: f1, f20
	public final String toString() {
		return "mv" + uIdVal.toString();
	}

	public UUID getUUIdVal() {
		return uIdVal;
	}

	@Override
	public boolean equals(BaseId id) {
		return ((id.getClass() == this.getClass()) && (((ModelVersionID) id)
				.getUUIdVal().compareTo(this.getUUIdVal()) == 0));
	}

	@Override
	public String getEntryNameFromDatabase(StorageI storage) throws Exception {
		ModelVersion borrowedObj = ((ModelVersion) ModelVersion
				.getObjectPoolFactory().borrowObject());
		String name = borrowedObj.getName(this, storage);
		ModelVersion.getObjectPoolFactory().returnObject(borrowedObj);
		return name;
	}

	@Override
	public BaseId[] getChildrenObjectIds(StorageI storage) throws Exception {
		ModelVersion borrowedObj = ((ModelVersion) ModelVersion
				.getObjectPoolFactory().borrowObject());
		ScenarioCollectionID[] scIds = borrowedObj
				.getScenarioCollectionIds(this, storage);
		ModelVersion.getObjectPoolFactory().returnObject(borrowedObj);
		return scIds;
	}

	@Override
	public boolean isEntity() {
		return true;
	}

	@Override
	public BaseId getParentObjectId(StorageI storage) throws Exception {
		ModelVersion borrowedObj = ((ModelVersion) ModelVersion
				.getObjectPoolFactory().borrowObject());
		ModelProjectID mpId = borrowedObj.getParentProjectId(this, storage);
		ModelVersion.getObjectPoolFactory().returnObject(borrowedObj);
		return mpId;
	}

	@Override
	public boolean hasChildren() {
		return true;
	}

	public void accept(SilverIDVisitor v) throws Exception {
		v.visit(this);
	}

	@Override
	public String getCreatorNameFromDatabase(StorageI storage) throws Exception {
		ModelVersion borrowedObj = ((ModelVersion) ModelVersion
				.getObjectPoolFactory().borrowObject());
		String creator = borrowedObj.getCreator(this, storage);
		ModelVersion.getObjectPoolFactory().returnObject(borrowedObj);
		return creator;
	}

	@Override
	public boolean setEntryNameToDatabase(String updateName, StorageI storage) throws Exception {
		ModelVersion borrowedObj = ((ModelVersion) ModelVersion
				.getObjectPoolFactory().borrowObject());
		boolean result = borrowedObj.setName(this, updateName, storage);
		ModelVersion.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	/**
	 * Get the name of the source file for this ModelVersion
	 * @param storage TODO
	 * 
	 * @return Name of the source file.
	 */
	public String getSourceFileName(StorageI storage) throws Exception {
		ModelVersion borrowedObj = ((ModelVersion) ModelVersion
				.getObjectPoolFactory().borrowObject());
		String result = borrowedObj.getSourceFileName(this, storage);
		ModelVersion.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	/**
	 * Get the simulation type associated with this ModelVersion from database
	 */
	@Override
	public String getTypeFromDatabase(StorageI storage) throws Exception {
		ModelVersion borrowedObj = ((ModelVersion) ModelVersion
				.getObjectPoolFactory().borrowObject());
		String result = borrowedObj.getType(this, storage);
		ModelVersion.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	/**
	 * Get the ModelVersion description from database
	 */
	@Override
	public String getDescriptionFromDatabase(StorageI storage) throws Exception {
		ModelVersion borrowedObj = ((ModelVersion) ModelVersion
				.getObjectPoolFactory().borrowObject());
		String result = borrowedObj.getDescription(this, storage);
		ModelVersion.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public boolean setDescriptionToDatabase(String desc, StorageI storage) throws Exception {
		ModelVersion borrowedObj = ((ModelVersion) ModelVersion
				.getObjectPoolFactory().borrowObject());
		boolean result = borrowedObj.setDescription(this, desc, storage);
		ModelVersion.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public Date getCreationTime(StorageI storage) throws Exception {
		ModelVersion borrowedObj = ((ModelVersion) ModelVersion
				.getObjectPoolFactory().borrowObject());
		Date result = borrowedObj.getCreationTime(this, storage);
		ModelVersion.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public boolean deleteFromDatabase(StorageI storage) throws Exception {
		ModelVersion borrowedObj = ((ModelVersion) ModelVersion
				.getObjectPoolFactory().borrowObject());
		boolean result = borrowedObj.deleteVersion(this, storage);
		ModelVersion.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

}
